<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the normalize method from the Base64Codec class, for the Dart programming language.">
  <title>normalize method - Base64Codec class - dart:convert library - Dart API</title>
  <!-- required because all the links are pseudo-absolute -->
  <base href="../..">

  <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500,400i,400,300|Source+Sans+Pro:400,300,700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="static-assets/github.css">
  <link rel="stylesheet" href="static-assets/styles.css">
  <link rel="icon" href="static-assets/favicon.png">
  
</head>

<body>

<div id="overlay-under-drawer"></div>

<header id="title">
  <button id="sidenav-left-toggle" type="button">&nbsp;</button>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="index.html">move_to_background</a></li>
    <li><a href="dart-convert/dart-convert-library.html">dart:convert</a></li>
    <li><a href="dart-convert/Base64Codec-class.html">Base64Codec</a></li>
    <li class="self-crumb">normalize method</li>
  </ol>
  <div class="self-name">normalize</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<main>

  <div id="dartdoc-sidebar-left" class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
      <form class="search-sidebar" role="search">
        <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
      </form>
    </header>
    
    <ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
      <li><a href="index.html">move_to_background</a></li>
      <li><a href="dart-convert/dart-convert-library.html">dart:convert</a></li>
      <li><a href="dart-convert/Base64Codec-class.html">Base64Codec</a></li>
      <li class="self-crumb">normalize method</li>
    </ol>
    
    <h5>Base64Codec class</h5>
    <ol>
      <li class="section-title"><a href="dart-convert/Base64Codec-class.html#constructors">Constructors</a></li>
      <li><a href="dart-convert/Base64Codec/Base64Codec.html">Base64Codec</a></li>
      <li><a href="dart-convert/Base64Codec/Base64Codec.urlSafe.html">urlSafe</a></li>
    
      <li class="section-title">
        <a href="dart-convert/Base64Codec-class.html#instance-properties">Properties</a>
      </li>
      <li><a href="dart-convert/Base64Codec/decoder.html">decoder</a></li>
      <li><a href="dart-convert/Base64Codec/encoder.html">encoder</a></li>
      <li class="inherited"><a href="dart-core/Object/hashCode.html">hashCode</a></li>
      <li class="inherited"><a href="dart-convert/Codec/inverted.html">inverted</a></li>
      <li class="inherited"><a href="dart-core/Object/runtimeType.html">runtimeType</a></li>
    
      <li class="section-title"><a href="dart-convert/Base64Codec-class.html#instance-methods">Methods</a></li>
      <li><a href="dart-convert/Base64Codec/decode.html">decode</a></li>
      <li><a href="dart-convert/Base64Codec/normalize.html">normalize</a></li>
      <li class="inherited"><a href="dart-convert/Codec/encode.html">encode</a></li>
      <li class="inherited"><a href="dart-convert/Codec/fuse.html">fuse</a></li>
      <li class="inherited"><a href="dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
      <li class="inherited"><a href="dart-core/Object/toString.html">toString</a></li>
    
      <li class="section-title inherited"><a href="dart-convert/Base64Codec-class.html#operators">Operators</a></li>
      <li class="inherited"><a href="dart-core/Object/operator_equals.html">operator ==</a></li>
    
    
    
    </ol>
  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-main-content" class="col-xs-12 col-sm-9 col-md-8 main-content">
      <div><h1><span class="kind-method">normalize</span> method</h1></div>

    <section class="multi-line-signature">
      <span class="returntype"><a href="dart-core/String-class.html">String</a></span>
            <span class="name ">normalize</span>
(<wbr><span class="parameter" id="normalize-param-source"><span class="type-annotation"><a href="dart-core/String-class.html">String</a></span> <span class="parameter-name">source</span>, [</span> <span class="parameter" id="normalize-param-start"><span class="type-annotation"><a href="dart-core/int-class.html">int</a></span> <span class="parameter-name">start</span> = <span class="default-value">0</span>, </span> <span class="parameter" id="normalize-param-end"><span class="type-annotation"><a href="dart-core/int-class.html">int</a></span> <span class="parameter-name">end</span></span> ])
      
    </section>
    <section class="desc markdown">
      <p>Validates and normalizes the base64 encoded data in <code>source</code>.</p>
<p>Only acts on the substring from <code>start</code> to <code>end</code>, with <code>end</code>
defaulting to the end of the string.</p>
<p>Normalization will:</p><ul><li>Unescape any <code>%</code>-escapes.</li><li>Only allow valid characters (<code>A</code>-<code>Z</code>, <code>a</code>-<code>z</code>, <code>0</code>-<code>9</code>, <code>/</code> and <code>+</code>).</li><li>Normalize a <code>_</code> or <code>-</code> character to <code>/</code> or <code>+</code>.</li><li>Validate that existing padding (trailing <code>=</code> characters) is correct.</li><li>If no padding exists, add correct padding if necessary and possible.</li><li>Validate that the length is correct (a multiple of four).</li></ul>
    </section>
    
    <section class="summary source-code" id="source">
      <h2><span>Implementation</span></h2>
      <pre class="language-dart"><code class="language-dart">String normalize(String source, [int start = 0, int end]) {
  end = RangeError.checkValidRange(start, end, source.length);
  const percent = 0x25;
  const equals = 0x3d;
  StringBuffer buffer;
  var sliceStart = start;
  var alphabet = _Base64Encoder._base64Alphabet;
  var inverseAlphabet = _Base64Decoder._inverseAlphabet;
  var firstPadding = -1;
  var firstPaddingSourceIndex = -1;
  var paddingCount = 0;
  for (var i = start; i &lt; end;) {
    var sliceEnd = i;
    var char = source.codeUnitAt(i++);
    var originalChar = char;
    &#47;&#47; Normalize char, keep originalChar to see if it matches the source.
    if (char == percent) {
      if (i + 2 &lt;= end) {
        char = parseHexByte(source, i); &#47;&#47; May be negative.
        i += 2;
        &#47;&#47; We know that %25 isn&#39;t valid, but our table considers it
        &#47;&#47; a potential padding start, so skip the checks.
        if (char == percent) char = -1;
      } else {
        &#47;&#47; An invalid HEX escape (too short).
        &#47;&#47; Just skip past the handling and reach the throw below.
        char = -1;
      }
    }
    &#47;&#47; If char is negative here, hex-decoding failed in some way.
    if (0 &lt;= char &amp;&amp; char &lt;= 127) {
      var value = inverseAlphabet[char];
      if (value &gt;= 0) {
        char = alphabet.codeUnitAt(value);
        if (char == originalChar) continue;
      } else if (value == _Base64Decoder._padding) {
        &#47;&#47; We have ruled out percent, so char is &#39;=&#39;.
        if (firstPadding &lt; 0) {
          &#47;&#47; Mark position in normalized output where padding occurs.
          firstPadding = (buffer?.length ?? 0) + (sliceEnd - sliceStart);
          firstPaddingSourceIndex = sliceEnd;
        }
        paddingCount++;
        &#47;&#47; It could have been an escaped equals (%3D).
        if (originalChar == equals) continue;
      }
      if (value != _Base64Decoder._invalid) {
        buffer ??= StringBuffer();
        buffer.write(source.substring(sliceStart, sliceEnd));
        buffer.writeCharCode(char);
        sliceStart = i;
        continue;
      }
    }
    throw FormatException(&quot;Invalid base64 data&quot;, source, sliceEnd);
  }
  if (buffer != null) {
    buffer.write(source.substring(sliceStart, end));
    if (firstPadding &gt;= 0) {
      &#47;&#47; There was padding in the source. Check that it is valid:
      &#47;&#47; * result length a multiple of four
      &#47;&#47; * one or two padding characters at the end.
      _checkPadding(source, firstPaddingSourceIndex, end, firstPadding,
          paddingCount, buffer.length);
    } else {
      &#47;&#47; Length of last chunk (1-4 chars) in the encoding.
      var endLength = ((buffer.length - 1) % 4) + 1;
      if (endLength == 1) {
        &#47;&#47; The data must have length 0, 2 or 3 modulo 4.
        throw FormatException(&quot;Invalid base64 encoding length &quot;, source, end);
      }
      while (endLength &lt; 4) {
        buffer.write(&quot;=&quot;);
        endLength++;
      }
    }
    return source.replaceRange(start, end, buffer.toString());
  }
  &#47;&#47; Original was already normalized, only check padding.
  var length = end - start;
  if (firstPadding &gt;= 0) {
    _checkPadding(source, firstPaddingSourceIndex, end, firstPadding,
        paddingCount, length);
  } else {
    &#47;&#47; No padding given, so add some if needed it.
    var endLength = length % 4;
    if (endLength == 1) {
      &#47;&#47; The data must have length 0, 2 or 3 modulo 4.
      throw FormatException(&quot;Invalid base64 encoding length &quot;, source, end);
    }
    if (endLength &gt; 1) {
      &#47;&#47; There is no &quot;insertAt&quot; on String, but this works as well.
      source = source.replaceRange(end, end, (endLength == 2) ? &quot;==&quot; : &quot;=&quot;);
    }
  }
  return source;
}</code></pre>
    </section>

  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-right" class="col-xs-6 col-sm-6 col-md-2 sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    move_to_background 0.1.0
  </span>

  
</footer>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="static-assets/typeahead.bundle.min.js"></script>
<script src="static-assets/highlight.pack.js"></script>
<script src="static-assets/URI.js"></script>
<script src="static-assets/script.js"></script>


</body>

</html>
